package com.yami.shop.user.common.service.impl;


import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.bo.PayInfoResultBO;
import com.yami.shop.bean.enums.PayStatus;
import com.yami.shop.bean.model.PayInfo;
import com.yami.shop.bean.pay.RefundInfoDto;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.service.PayInfoService;
import com.yami.shop.service.RefundInfoService;
import com.yami.shop.user.common.dao.UserGasLogMapper;
import com.yami.shop.user.common.model.UserGasInfo;
import com.yami.shop.user.common.model.UserGasLog;
import com.yami.shop.user.common.model.UserLevelLog;
import com.yami.shop.user.common.service.UserGasInfoService;
import com.yami.shop.user.common.service.UserGasLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.Objects;



@Slf4j
@Service
public class UserGasLogServiceImpl extends ServiceImpl<UserGasLogMapper, UserGasLog> implements UserGasLogService {

    @Autowired
    private  RefundInfoService refundInfoService;
    @Autowired
    private  PayInfoService payInfoService;
    @Autowired
    private  Snowflake snowflake;
    @Autowired
    private UserGasLogMapper userGasLogMapper;
    @Autowired
    private UserGasInfoService userGasInfoService;

    @Override
    public ServerResponseEntity<String> noticeGasRecharge(PayInfoResultBO payInfoResultBO, PayInfo payInfo) {
        Long gasLogId = Long.valueOf(payInfo.getOrderNumbers());
        UserGasLog userGasLog = this.getById(gasLogId);
        if (Objects.isNull(userGasLog)) {
            log.info("GAS余额充值订单信息异常,支付单号:" + payInfo.getPayNo());
            return ServerResponseEntity.success(payInfoResultBO.getSuccessString());
        }
        if (Objects.equals(userGasLog.getIsPayed(), 1)) {
            RefundInfoDto refundInfo = new RefundInfoDto();
            refundInfo.setRefundSn(String.valueOf(snowflake.nextId()));
            refundInfo.setRefundAmount(payInfo.getPayAmount());
            refundInfo.setPayNo(payInfo.getPayNo());
            refundInfo.setRefundOrderNumbers(Collections.singletonList(String.valueOf(gasLogId)));
            refundInfo.setOnlyRefund(1);
            refundInfoService.doRefund(refundInfo);
            // 标记为退款
            payInfoService.markerRefund(payInfo.getPayNo());
            return ServerResponseEntity.success(payInfoResultBO.getSuccessString());
        }
        // 支付成功回调
        PayInfo newPayInfo = new PayInfo();
        newPayInfo.setPayNo(payInfo.getPayNo());
        newPayInfo.setBizPayNo(payInfoResultBO.getBizPayNo());
        newPayInfo.setCallbackContent(payInfoResultBO.getCallbackContent());
        newPayInfo.setCallbackTime(new Date());
        newPayInfo.setPayStatus(PayStatus.PAYED.value());
        payInfoService.update(newPayInfo, new LambdaUpdateWrapper<PayInfo>().eq(PayInfo::getPayNo, newPayInfo.getPayNo()));
        // 修改为已支付
        userGasLog.setIsPayed(1);
        this.updateById(userGasLog);
        //用户标识 默认0 用户  1 店铺
        if(userGasLog.getUserTab().equals("0")){
            // 添加用户gas 余额
            LambdaQueryWrapper<UserGasInfo> eq = new LambdaQueryWrapper<UserGasInfo>().eq(UserGasInfo::getUserId, userGasLog.getUserId());
            UserGasInfo userGasInfo = userGasInfoService.getOne(eq);
            if(ObjectUtils.isNotEmpty(userGasInfo)){
                userGasInfo.setGasNumber(userGasInfo.getGasNumber()+userGasLog.getGasNum());
                userGasInfoService.updateById(userGasInfo);
            }else{
                userGasInfo = new UserGasInfo();
                userGasInfo.setGasNumber(userGasLog.getGasNum());
                userGasInfo.setUserId(userGasLog.getUserId());
                userGasInfoService.save(userGasInfo);
            }
        }
        return ServerResponseEntity.success(payInfoResultBO.getSuccessString());
    }

    @Override
    public PageVO<UserGasLog> getGasDetailed(PageDTO pageDTO, String year, String month, String userId) {
        PageVO<UserGasLog> pagelist = PageUtil.doPage(pageDTO, () -> userGasLogMapper.selectByMonth(year,month,userId));

        return pagelist;
    }

    @Override
    public UserGasLog getGasPayInfoByUserId(String userId) {
        UserGasLog gasPayInfoByUserId = userGasLogMapper.getGasPayInfoByUserId(userId);
        return gasPayInfoByUserId;
    }


    @Override
    public void saveGasNumberThansferByUserId(String userId, int gasNumber, Integer type,String channelCode,String chainCode,String toUserId) {
        userGasInfoService.saveGasNumberByUserId(userId,gasNumber);
        UserGasLog userGasLog = new UserGasLog();
        userGasLog.setCreateTime(new Date());
        userGasLog.setIoType(1); // 收支类型 0支出 1收入
        userGasLog.setType(type);   // gas 充值类型
        userGasLog.setIsPayed(1);  // 0未支付
        userGasLog.setUserId(userId);
        userGasLog.setTabStatus(1);
        userGasLog.setPateTab(channelCode);  //充值平台
        userGasLog.setChainTab(chainCode);
        userGasLog.setToUserId(toUserId);
        userGasLog.setGasNum(gasNumber);
        userGasLogMapper.insert(userGasLog);
    }

    @Override
    public void reduceGasNumberThansferByUserId(String userId, int gasNumber, Integer type,String channelCode,String chainCode,String toUserId) {
        userGasInfoService.reduceGasNumberByUserId(userId,gasNumber);
        UserGasLog userGasLog = new UserGasLog();
        userGasLog.setCreateTime(new Date());
        userGasLog.setIoType(0); // 收支类型 0支出 1收入
        userGasLog.setType(type);   // gas 充值类型
        userGasLog.setIsPayed(1);  // 0未支付
        userGasLog.setUserId(userId);
        userGasLog.setTabStatus(1);
        userGasLog.setPateTab(channelCode);  //充值平台
        userGasLog.setChainTab(chainCode);
        userGasLog.setToUserId(toUserId);
        userGasLog.setGasNum(gasNumber);
        userGasLogMapper.insert(userGasLog);
    }
}
